package cn.mujiankeji.mbrowser.数据.数据库

import androidx.room.*
import kotlinx.coroutines.flow.Flow
import java.util.UUID

@Entity
data class DBQzmBag(

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Long = 0L,

    @ColumnInfo(name = "uid")
    var uid: String = UUID.randomUUID().toString(),

    @ColumnInfo(name = "name")
    var 名称: String,

    @ColumnInfo(name = "url")
    var 订阅地址: String = "",

    @ColumnInfo(name = "version")
    var 版本: Int = 0,

    @ColumnInfo(name = "enabled")
    var 启用: Boolean = true,

    @ColumnInfo(name = "create_time")
    var 创建时间: Long = System.currentTimeMillis(),

    @ColumnInfo(name = "update_time")
    var 更新时间: Long = System.currentTimeMillis()
)

@Dao
interface DaoQzmBag {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun 插入订阅(订阅: DBQzmBag): Long

    @Update
    suspend fun 更新订阅(订阅: DBQzmBag)

    @Delete
    suspend fun 删除订阅(订阅: DBQzmBag)

    // 检测名称可否使用
    @Query("SELECT COUNT(*) FROM DBQzmBag WHERE name = :name")
    suspend fun 检测名称可否使用(name: String): Int


    // 清空
    @Query("DELETE FROM DBQzmBag")
    suspend fun 清空()

    @Query("SELECT * FROM DBQzmBag WHERE id = :编号")
    suspend fun 获取订阅包(编号: Long): DBQzmBag?

    // 根据uuid 获取包
    @Query("SELECT * FROM DBQzmBag WHERE uid = :uuid")
    suspend fun 获取订阅包(uuid: String): DBQzmBag?

    @Query("SELECT * FROM DBQzmBag WHERE enabled = 1")
    fun 获取所有启用订阅(): Flow<List<DBQzmBag>>

    @Query("SELECT * FROM DBQzmBag")
    fun 获取所有订阅Flow(): Flow<List<DBQzmBag>>

    @Query("SELECT * FROM DBQzmBag")
    suspend fun 获取所有订阅(): List<DBQzmBag>?
}

